package com.itcam.phaseiii.thread_.producer_consumer;

/**
 * Author:Cammy.Wu
 * Date:2024-05-14
 * Description:生产者
 */

public class Producer implements Runnable {

    /**
     * When an object implementing interface {@code Runnable} is used
     * to create a thread, starting the thread causes the object's
     * {@code run} method to be called in that separately executing
     * thread.
     * <p>
     * The general contract of the method {@code run} is that it may
     * take any action whatsoever.
     *
     * @see Thread#run()
     */
    @Override
    public void run() {
        while (true) {
            WareHouse.lock.lock();

            if (WareHouse.mark) {
                // true:说明有包子，线程进入等待状态
                try {
                    WareHouse.producer.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            } else {
                // false:没有包子，生成包子，改变mark的状态，唤醒消费者线程
                System.out.println("生产者线程生成包子...");
                WareHouse.mark=true;
                WareHouse.consumer.signal();
            }

            WareHouse.lock.unlock();
        }
    }
}
